﻿using System;
using System.Linq;
using CommonBehaviors.Actions;
using FarmerJoe.Helpers.Logging;
using Styx;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;

namespace FarmerJoe.Helpers
{
    class Inventory
    {
        public static LocalPlayer Me { get { return StyxWoW.Me; } }
        public static WoWUnit CT { get { return Me.CurrentTarget; } }


        public static Composite UseTrinket1()
        {
            return new PrioritySelector(
                new Decorator( ret => (ClassParser.Trinket1),
                    new PrioritySelector( ctx => StyxWoW.Me.Inventory.GetItemBySlot((uint) WoWInventorySlot.Trinket1),
                        new Decorator(ctx => ctx != null && CanUseEquippedItem((WoWItem) ctx),
                            new Sequence(
                                      new Action(ctx => Use((WoWItem) ctx)),
                                      new WaitContinue(1,ret=> false, new ActionAlwaysSucceed()),
                                      new Decorator(ret => Me.CurrentPendingCursorSpell != null,
                                          new Sequence(
                                              new Action(context => Log.Write(Colours.InformativeColour,"The trinket appears to have a ground targeted spell, clicking target's location")),
                                              new Action(ret => SpellManager.ClickRemoteLocation(CT.Location)),
                                              new WaitContinue(1, ret => false, new ActionAlwaysSucceed())
                                              ))
                                      
                                      
                            )
                        )
                        )
                    )
                );
        }

        public static Composite UseTrinket2()
        {
            return new PrioritySelector(
                new Decorator( ret => ClassParser.Trinket2,
                    new PrioritySelector(ctx => StyxWoW.Me.Inventory.GetItemBySlot((uint) WoWInventorySlot.Trinket2),
                        new Decorator(ctx => ctx != null && CanUseEquippedItem((WoWItem) ctx),
                             new Sequence(
                                      new Action(ctx => Use((WoWItem)ctx)),
                                      new WaitContinue(1, ret => false, new ActionAlwaysSucceed()),
                                      new Decorator(ret => Me.CurrentPendingCursorSpell != null,
                                          new Sequence(
                                              new Action(context => Log.Write(Colours.InformativeColour, "The trinket appears to have a ground targeted spell, clicking target's location")),
                                              new Action(ret => SpellManager.ClickRemoteLocation(CT.Location)),
                                              new WaitContinue(1, ret => false, new ActionAlwaysSucceed())
                                              ))


                            )
                            )
                        )
                    )
                );
        }
        private static bool CanUseEquippedItem(WoWItem item)
        {
            // Check for engineering tinkers!
            string itemSpell = Lua.GetReturnVal<string>("return GetItemSpell(" + item.Entry + ")", 0);
            if (string.IsNullOrEmpty(itemSpell)) return false;

            return item.Usable && item.Cooldown <= 0;
        }

        public static WoWItem GetItem(string itemName)
        {
            return Me.BagItems.FirstOrDefault(item => item.Name.ToUpper() == itemName.ToUpper());
        }

        public static bool HaveItem(string itemName)
        {
            return Me.BagItems.Any(item => item.Name.ToUpper() == itemName.ToUpper());
        }

        public static bool HaveItem(int itemNumber)
        {
            return Me.BagItems.Any(item => item.ItemInfo.Id == itemNumber);
        }

        public static bool HaveItem(uint itemNumber)
        {
            return Me.BagItems.Any(item => item.ItemInfo.Id == itemNumber);
        }

        public static bool IsUseable(WoWItem item)
        {
            //get
            {
                if (item == null) return false;

                string luacode = String.Format("return GetItemCooldown(\"{0}\")", item.Entry);
                return Misc.LuaGetReturnValueString(luacode) == "0";
            }
        }

        public static void Use(WoWItem item)
        {
            //if (Me.IsCasting) Spell.StopCasting();
            //Utils.LagSleep();

            //WoWItem manaPotion = ManaPotion;
            Logging.Log.Write("Using item " + item.Name);
            //Utils.Log(string.Format("We're having an 'Oh Shit' moment. Using {0}", item.Name), Utils.Colour("Red"));
            item.Interact();
        }
    }
}
